#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
#define sort stable_sort
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,ubuf[1<<23],*u=ubuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
signed main()
{
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	vector<int> pw(31,1); rep(i,1,30) pw[i]=pw[i-1]*3;
	function<int(int)> get_full=[&](int n)->int
	{
		if(!n) return 1;
		int bit=63-__builtin_clzll(n),rem=n-(1<<bit);
		return pw[bit]+get_full(rem)*2;
	};
	auto get_part=[&](int n,int t)->int
	{
		int dp1[2][2]={{0,0},{0,1}},dp2[2][2];
		req(bit,31,0)
		{
			dp2[0][0]=dp2[0][1]=dp2[1][0]=dp2[1][1]=0;
			int nb=(n>>bit)&1,tb=(t>>bit)&1;
			rep(nn,0,1) rep(nt,0,1) rep(bit1,0,1)
			{
				if(nn&&bit1>nb) continue;
				rep(bit2,0,bit1)
				{
					if(nt&&bit2>tb) continue;
					dp2[nn&&bit1==nb][nt&&bit2==tb]+=dp1[nn][nt];
				}
			}
			dp1[0][0]=dp2[0][0],dp1[0][1]=dp2[0][1],dp1[1][0]=dp2[1][0],dp1[1][1]=dp2[1][1];
		}
		return dp1[0][0]+dp1[0][1]+dp1[1][0]+dp1[1][1];
	};
	int q,l,r; read(q); while(q--) read(l,r),writeln((get_full(r)-get_full(l-1))-(get_part(r,l-1)-get_part(l-1,l-1)));
	return 0;
}
